<?php


// 其他响应内容或处理逻辑
namespace app\controller;

use think\facade\Request;
use app\BaseController;
use think\facade\Db;
use think\response\Json;

class Mysql extends BaseController
{
    public function sayHello()
    {
        return 'hello';
    }

    //获取所有
    public function mysql_get_all(string $input_table_name)
    {
        $user = Db::name($input_table_name)->select();
        return json($user);
    }

    //根据id查找值
    public function mysql_get_bygid(string $input_table_name)
    {
        $input_paperid = Request::param('input_paperid');
        $user = Db::name($input_table_name)->where("pid", $input_paperid)->findOrEmpty();
        if ($user) {
            return json($user);
        } else {
            return ('null');
        }
    }

    //根据name查找值
    public function mysql_get_bygname(string $input_table_name)
    {
        $input_papername = Request::param('input_papername');
        $user = Db::name($input_table_name)->where("pname", $input_papername)->findOrEmpty();
        if ($user) {
            return json($user);
        } else {
            return ('null');
        }
    }

    //增加或更改信息
    public function mysql_add_paper()
    {
        $paper_table_name = "报纸信息";
        $input_paperid = Request::param('input_paperid');
        $input_papername = Request::param('input_papername');
        $input_paperprice = Request::param('input_paperprice');
        $input_papernorm = Request::param('input_papernorm');
        $input_papernuit = Request::param('input_papernuit');

        $test = Db::name($paper_table_name)->where("pid", $input_paperid)->find();
        if ($test) {
            $updata = ["pname" => $input_papername, "pprice" => $input_paperprice, "pnorm" => $input_papernorm, "punit" => $input_papernuit];
            Db::name($paper_table_name)->where("pid", $input_paperid)->update($updata);
        } else {
            $updata = ["pid" => $input_paperid, "pname" => $input_papername, "pprice" => $input_paperprice, "pnorm" => $input_papernorm, "punit" => $input_papernuit];
            Db::name($paper_table_name)->insert($updata);
        }
        return 'success';
    }

    //删除数据
    public function query_delete_paper()
    {
        $book_table_name = "报纸信息";
        $input_paperid = Request::param('input_paperid');
        $test = Db::name($book_table_name)->where("pid", $input_paperid)->find();
        if ($test) {
            Db::name($book_table_name)->where('pid', $input_paperid)->delete(true);
        } else {
            //没有该书籍
            return 'error null';
        }
        return 'success';
    }

    //添加用户
    public function mysql_add_customer()
    {
        $customer_table_name = "客户信息";
        $input_id = Request::param('input_id');
        $input_name = Request::param('input_name');
        $input_phone = Request::param('input_phone');
        $input_address = Request::param('input_address');
        $input_number = Request::param('input_number');
        $test = Db::name($customer_table_name)->where("cid", $input_id)->find();
        if ($test) {
            return "account repeat";
        } else {
            $updata = ["cid" => $input_id, "cname" => $input_name, "cphone" => $input_phone, "caddress" => $input_address, "cnumber" => $input_number];
            Db::name($customer_table_name)->insert($updata);
            return 'success';
        }
    }

    //订阅报纸反馈给用户信息
    public function subscribe()
    {

        $customer_table_name = "客户信息";
        $subscribe_table_name = "订阅信息";
        $book_table_name = "报纸信息";
        $input_id = Request::param('input_id');
        $input_paperid = Request::param('input_paperid');
        $input_papernum = Request::param('input_papernum');

        $now = date('Y-m-d');
        $user_data = Db::name($customer_table_name)->where("cid", $input_id)->find();
        if ($user_data) {
            $paper_data = Db::name($book_table_name)->where("pid", $input_paperid)->find();
            if ($paper_data) {
                $updata = ["cid" => $user_data["cid"], "pid" => $paper_data["pid"], "num" => $input_papernum, "price" => $paper_data["pprice"], "date" => $now, "totalprice" => $paper_data["pprice"] * $input_papernum];
                return json($updata);
            } else {
                return ("Null paper");
            }
        } else {
            return ('Null user');
        }
    }

    //确定（实际上这个才是主要）
    public function ok()
    {
        $customer_table_name = "客户信息";
        $subscribe_table_name = "订阅信息";
        $book_table_name = "报纸信息";
        $input_id = Request::param('input_id');
        $input_paperid = Request::param('input_paperid');
        $input_papernum = Request::param('input_papernum');

        $now = date('Y-m-d');
        $user_data = Db::name($customer_table_name)->where("cid", $input_id)->find();
        if ($user_data) {
            $paper_data = Db::name($book_table_name)->where("pid", $input_paperid)->find();
            if ($paper_data) {
                $updata = ["cid" => $user_data["cid"], "pid" => $paper_data["pid"], "num" => $input_papernum, "price" => $paper_data["pprice"], "date" => $now, "totalprice" => $paper_data["pprice"] * $input_papernum];
                Db::name($subscribe_table_name)->insert($updata);
                return ("success");
            } else {
                return ("Null paper");
            }
        } else {
            return ('Null user');
        }
    }

    //返回用户所有信息
    public function get_userdata()
    {
        $customer_table_name = "客户信息";
        $subscribe_table_name = "订阅信息";
        $book_table_name = "报纸信息";
        $input_id = Request::param('input_id');
        $input_name = Request::param('input_name');
        if ($input_id != "") {
            $user_data = Db::name($customer_table_name)->where("cid", $input_id)->find();
            if ($user_data) {
                return json($user_data);
            } else {
                return ("Null user");
            }
        } else {
            $user_data = Db::name($customer_table_name)->where("cname", $input_name)->find();
            if ($user_data) {
                return json($user_data);
            } else {
                return ("Null user");
            }
        }
    }

    //返回用户所有订阅
    public function get_usersub()
    {
        $customer_table_name = "客户信息";
        $subscribe_table_name = "订阅信息";
        $book_table_name = "报纸信息";
        $input_id = Request::param('input_id');
        $input_name = Request::param('input_name');
        if ($input_id != "") {
            $user_data = Db::name($customer_table_name)->where("cid", $input_id)->find();
            if ($user_data) {
                $user_sub = Db::name($subscribe_table_name)->where("cid", $input_id)->select();
                return json($user_sub);
            } else {
                return ("Null user");
            }
        } else {
            $user_data = Db::name($customer_table_name)->where("cname", $input_name)->find();
            if ($user_data) {
                $user_sub = Db::name($subscribe_table_name)->where("cid", $user_data['cid'])->select();
                return json($user_sub);
            } else {
                return ("Null user");
            }
        }
    }
}
